import {computed} from "vue"
import {getFormFieldShowName} from "components/NewCom/WkForm/utils.js";
import {crmLeadsStarAPI} from "@/api/crm/leads.js";
import {crmCustomerStarAPI} from "@/api/crm/customer.js";
import {crmContactsStarAPI} from "@/api/crm/contacts.js";
import {crmBusinessStarAPI} from "@/api/crm/business.js";
import {ElMessage} from "element-plus"

export function useSzColumn() {
  /**
   * 格式化字段
   * @param {*} row
   * @param {*} column
   * @param {*} cellValue
   */
  function fieldFormatter(row, column, cellValue, field) {
    // 如果需要格式化
    if (column.property === 'isTransform') {
      return ['否', '是'][row[column.property]] || '--'
    }

    if (field) {
      return getFormFieldShowName(field.formType, row[column.property], '--', field)
    }
    return row[column.property] === '' || row[column.property] === null ? '--' : row[column.property]
  }

  /**
   * 切换关注状态
   * @param index
   * @param status
   */
  function toggleStar(crmOption, data) {
    crmOption.loading = true

    const request = {
      leads: crmLeadsStarAPI,
      customer: crmCustomerStarAPI,
      contacts: crmContactsStarAPI,
      business: crmBusinessStarAPI
    }[crmOption.crmType]

    console.log("request", request)
    console.log("request", crmOption)
    console.log("request", crmOption.crmType)
    const params = {}
    params[`id`] = data[`${crmOption.crmType}Id`]

    request(params).then(() => {
      crmOption.loading = false
      ElMessage.success({message: data.star > 0 ? '取消关注成功' : '关注成功'})
      data.star = data.star > 0 ? 0 : 1
    }).catch((r) => {
      console.log("r", r)
      crmOption.loading = false
    })
  }


  function dealIcon(status) {
    return status == 1 ? 'wk wk-success deal-suc' : 'wk wk-close deal-un'
  }

  function dealName(status) {
    return status == 1 ? '已成交' : '未成交'
  }

  return {fieldFormatter, dealIcon, dealName, toggleStar}
}